﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.IotInfo;
using LJX.SmartPark.Domain.SafeManage;
using LJX.SmartPark.Domain.SysManage;
using LJX.SmartPark.Service.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.SafeManage
{
    public class PatrolCollocateService : IPatrolCollocateService
    {
        private readonly IRepository<PatrolCollocate> _studentService;

        public PatrolCollocateService(IRepository<PatrolCollocate> studentService)
        {
            _studentService = studentService;
        }

        public async Task<Response<bool>> DeleteByIdAsync(PatrolCollocate input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<PatrolCollocate>());
            return Response<bool>.Ok(result > 0);
        }

        public async Task<Response<List<PatrolCollocate>>> GetListPatrolCollocateAsync()
        {
            var students = await _studentService.GetAllListAsync();
            var result = students.MapToList<PatrolCollocate>();
            return Response<List<PatrolCollocate>>.Ok(result);
        }

        public async Task<Response<List<PatrolCollocate>>> GetPatrolCollocateAsync(PatrolCollocateForm form)
        {
            var students = await _studentService.GetAllListAsync();
            if (form.pid != 0)
            {
                students = students.Where(x => x.HouseId == form.pid).ToList();
            }
            if(!string.IsNullOrEmpty(form.name))
            {
                students = students.Where(x=>x.Name.Contains(form.name)).ToList();
            }
            if (!string.IsNullOrEmpty(form.num))
            {
                students = students.Where(x => x.Num.Contains(form.num)).ToList();
            }
            var count = students.Count();
            students = students.Skip((form.pageIndex - 1) * form.pageSize).Take(form.pageSize).ToList();
            var result = students.MapToList<PatrolCollocate>();
            return Response<List<PatrolCollocate>>.Ok(result, count);
        }

        public async Task<Response<bool>> PostAddPatrolCollocate(PatrolCollocate input)
        {
            var result = _studentService.InsertAsyncById(input.MapTo<PatrolCollocate>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> PutUpdatePatrolCollocate(PatrolCollocate input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<PatrolCollocate>());

            return Response<bool>.Ok(result.Result > 0);
        }
    }
}
